﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace CDCOSDH
{
    public partial class FrmListbox : Form
    {
        private string subjectName;
        private string selectedSDH;
        private SortedDictionary<string, string> emneord = new SortedDictionary<string, string>();
        private string[] usrMailRightSelected;
        private string[] caseParts;
        
        public ListView listView;

        public FrmListbox(string subjectName, string[] usrMailRightSelected)
        {
            InitializeComponent();

            this.subjectName = subjectName;
            this.Text = "Vælg " + this.subjectName;
            this.usrMailRightSelected = usrMailRightSelected;

            this.listViewCollection.Select();
        }

        public FrmListbox(string subjectName, string[] usrMailRightSelected, string selectedSDH)
        {
            InitializeComponent();

            this.subjectName = subjectName;
            this.selectedSDH = selectedSDH;
            this.Text = this.subjectName;
            this.usrMailRightSelected = usrMailRightSelected;

            this.listViewCollection.Select();
        }

        public FrmListbox(string subjectName, string[] usrMailRightSelected, string[] caseParts, string selectedSDH)
        {
            InitializeComponent();

            this.subjectName = subjectName;
            this.selectedSDH = selectedSDH;
            this.Text = this.subjectName;
            this.usrMailRightSelected = usrMailRightSelected;
            this.caseParts = caseParts;

            this.listViewCollection.Select();
        }

        private void FrmListbox_Load(object sender, EventArgs e)
        {
            if (this.subjectName == "hvem må se e-mailen i SDH")
            {
                this.lbAddCats.Visible = false;
                this.tbAddFreeCats.Visible = false;
                this.listViewCollection.Height = 427;

                ColumnHeader header = new ColumnHeader();
                header.Text = "Vælg person eller personer";
                header.Name = "Person/personer";
                this.listViewCollection.Columns.Add(header);
                string[] listMailInvitRigts; 
                listMailInvitRigts = ConvergensDominoConnector.DominoConnector.Default.LoadMailInvitRigts();

                foreach (string item in listMailInvitRigts)
                {
                    this.listViewCollection.Items.Add(new ListViewItem(item));
                }

                foreach (ListViewItem listItem in this.listViewCollection.Items)
                {
                    if (this.usrMailRightSelected.Any(s => s.Equals(listItem.Text)))
                    {
                        listItem.Checked = true;
                    }
                    else
                    {
                        listItem.Checked = false;
                    }
                }

                this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                this.listViewCollection.Update();
            }

            if (this.subjectName == "Frie kategorier")
            {
                this.lbAddCats.Visible = true;
                this.tbAddFreeCats.Visible = true;
                this.listViewCollection.Height = 390;

                ColumnHeader header = new ColumnHeader();
                header.Text = "Vælg frie kategorier";
                header.Name = "Frie kategorier";
                this.listViewCollection.Columns.Add(header);
                List<object> freeCats = ConvergensDominoConnector.DominoConnector.Default.FreeCatCEvaluate("UVM1", ConvergensDominoConnector.DominoConnector.Default.GetSDHDatabaseFilePath(this.selectedSDH));

                if (freeCats[0] != null)
                {

                    string[] stringarr = ((IEnumerable)freeCats).Cast<object>()
                                         .Select(x => x.ToString())
                                         .ToArray();


                    foreach (string item in stringarr)
                    {
                        this.listViewCollection.Items.Add(new ListViewItem(item));
                    }

                    //for (int i = 0; i < this.usrMailRightSelected.Length; i++)
                    //{
                    //    foreach (ListViewItem listItem in this.listViewCollection.Items)
                    //    {
                    //        if (this.usrMailRightSelected[i].Equals(listItem.Text))
                    //        {
                    //            listItem.Checked = true;
                    //        }
                    //        else
                    //        {
                    //            listItem.Checked = false;
                    //        }
                    //    }
                    //}

                    foreach (ListViewItem listItem in this.listViewCollection.Items)
                    {
                        if (this.usrMailRightSelected.Any(s => s.Equals(listItem.Text)))
                        {
                            listItem.Checked = true;
                        }
                        else
                        {
                            listItem.Checked = false;
                        }
                    }

                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                    this.listViewCollection.Update();
                }
            }

            if (this.subjectName == "Vælg Sagsområde")
            {
                ColumnHeader header = new ColumnHeader();
                header.Text = "Sagsområde";
                header.Name = "Sagsområde";

                this.listViewCollection.Columns.Add(header);

                List<object> CaseAreas = ConvergensDominoConnector.DominoConnector.Default.GetCaseAreaVarivant2(this.selectedSDH);
                if (CaseAreas[0] != null)
                {

                    string[] stringarr = ((IEnumerable)CaseAreas).Cast<object>()
                                         .Select(x => x.ToString())
                                         .ToArray();


                    foreach (string item in stringarr)
                    {
                        this.listViewCollection.Items.Add(new ListViewItem(item));
                    }

                    foreach (ListViewItem listItem in this.listViewCollection.Items)
                    {
                        if (this.usrMailRightSelected.Any(s => s.Equals(listItem.Text)))
                        {
                            listItem.Checked = true;
                        }
                        else
                        {
                            listItem.Checked = false;
                        }
                    }

                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                    this.listViewCollection.Update();
                }
            }

            if (this.subjectName == "Vælg Sagsdele")
            {
                this.lbAddCats.Visible = true;
                this.tbAddFreeCats.Visible = true;
                this.listViewCollection.Height = 390;
                this.lbAddCats.Text = "Tilføj sagsdele";

                ColumnHeader header = new ColumnHeader();
                header.Text = "Vælg Sagsdele";
                header.Name = "Sagsdele";
                this.listViewCollection.Columns.Add(header);
                List<string> caseParts = this.caseParts.ToList();

                if (caseParts[0] != null)
                {

                    string[] stringarr = ((IEnumerable)caseParts).Cast<object>()
                                         .Select(x => x.ToString())
                                         .ToArray();


                    foreach (string item in stringarr)
                    {
                        this.listViewCollection.Items.Add(new ListViewItem(item));
                    }

                    foreach (ListViewItem listItem in this.listViewCollection.Items)
                    {
                        if (this.usrMailRightSelected.Any(s => s.Equals(listItem.Text)))
                        {
                            listItem.Checked = true;
                        }
                        else
                        {
                            listItem.Checked = false;
                        }
                    }

                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.listViewCollection.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                    this.listViewCollection.Update();
                }
            }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.listView = this.listViewCollection;

            this.DialogResult = DialogResult.OK;
            
            //if (this.tbAddFreeCats.Text.Length > 0)
            //{
            //    this.listViewCollection.Items.Add(new ListViewItem(this.lbAddCats.Text.Trim()));
            //}
        }

        private void btnCnl_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void tbAddFreeCats_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.listViewCollection.Items.Add(new ListViewItem(this.tbAddFreeCats.Text.Trim()));
                this.tbAddFreeCats.Text = string.Empty;

                this.listView = this.listViewCollection;
            }
        }
    }
}
